package cn.xfdzx.android.apps.shop.fragment;

import android.content.res.ColorStateList;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.google.android.material.tabs.TabLayout;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import cn.xfdzx.android.apps.shop.R;
import cn.xfdzx.android.apps.shop.adapter.HomeVPAdapter;
import cn.xfdzx.android.apps.shop.base.BaseMvpFragment;
import cn.xfdzx.android.apps.shop.evenbus.CarInitDataMessage;
import cn.xfdzx.android.apps.shop.evenbus.CarPositionMessage;
import cn.xfdzx.android.apps.shop.evenbus.HomeTypeMessage;
import cn.xfdzx.android.apps.shop.fragment.car.CarCommunityRetailFragment;
import cn.xfdzx.android.apps.shop.fragment.car.CarMarketPurchaseFragment;
import cn.xfdzx.android.apps.shop.util.StatusBarUtil;
import cn.xfdzx.android.apps.shop.view.NoScrollViewPager;

import static cn.xfdzx.android.apps.shop.activity.MainActivity.TYPE_IS_RETAIL;

public class FourFragment extends BaseMvpFragment {


    LinearLayout llState;
    TabLayout mTabLayout;
    NoScrollViewPager vpFragmentFour;
    private RelativeLayout.LayoutParams item_bar;
    private List<Fragment> mFragments;
    private String[] title = {"零售", "批发"};
    private HomeVPAdapter homeVPAdapter;

    @Override
    protected void initView(View view) {

        llState = view.findViewById(R.id.ll_state);
        mTabLayout = view.findViewById(R.id.tl_fragment_four_title);
        vpFragmentFour = view.findViewById(R.id.vp_fragment_four);
        initHigh();
        EventBus.getDefault().register(this);

        mFragments = new ArrayList<>();
        mFragments.add(CarCommunityRetailFragment.newInstance(1));//社区零售
        mFragments.add(CarMarketPurchaseFragment.newInstance(1));//市场采购
        mTabLayout.setTabMode(TabLayout.MODE_FIXED);
        mTabLayout.setTabRippleColor(ColorStateList.valueOf(getContext().getResources().getColor(R.color.picture_color_transparent)));

        for (String tab : title) {
            mTabLayout.addTab(mTabLayout.newTab().setText(tab));
        }
        homeVPAdapter = new HomeVPAdapter(getChildFragmentManager(), title, mFragments);
        vpFragmentFour.setAdapter(homeVPAdapter);

        mTabLayout.setupWithViewPager(vpFragmentFour);

        if (TYPE_IS_RETAIL == 0) {
            mTabLayout.setTabTextColors(getResources().getColor(R.color.color_FB9129), getResources().getColor(R.color.color_FB9129));
            mTabLayout.setSelectedTabIndicatorColor(getResources().getColor(R.color.color_FB9129));
            vpFragmentFour.setCurrentItem(0);
        } else {
            mTabLayout.setTabTextColors(getResources().getColor(R.color.color_02C557), getResources().getColor(R.color.color_02C557));
            mTabLayout.setSelectedTabIndicatorColor(getResources().getColor(R.color.color_02C557));
            vpFragmentFour.setCurrentItem(1);
        }

        vpFragmentFour.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                //零售与批发状态更改
                TYPE_IS_RETAIL=position;
                EventBus.getDefault().post(new HomeTypeMessage(position));
                //零售批发数据加载
                EventBus.getDefault().post(new CarInitDataMessage(position));
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });


        initOnPreDrawListener();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_four;
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }
    /*
     * 方法名：initOnPreDrawListener()
     * 功    能：初始化viewTreeObserver事件监听,重写OnPreDrawListener获取组件高度
     * 参    数：无
     * 返回值：无
     */
    private void initOnPreDrawListener() {
        final ViewTreeObserver viewTreeObserver =  getActivity().getWindow().getDecorView().getViewTreeObserver();
        viewTreeObserver.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
            @Override
            public boolean onPreDraw() {
                EventBus.getDefault().post(new CarInitDataMessage(vpFragmentFour.getCurrentItem()));
                // 移除OnPreDrawListener事件监听
                getActivity().getWindow().getDecorView().getViewTreeObserver().removeOnPreDrawListener(this);

                return true;
            }
        });
    }
    @Override
    public void onDestroy() {
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
        super.onDestroy();
    }
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void XXX(HomeTypeMessage messageEvent) {
        if (messageEvent.getMessage() == 0) {
            mTabLayout.setTabTextColors(getResources().getColor(R.color.color_FB9129), getResources().getColor(R.color.color_FB9129));
            mTabLayout.setSelectedTabIndicatorColor(getResources().getColor(R.color.color_FB9129));
            vpFragmentFour.setCurrentItem(0);
        } else {
            mTabLayout.setTabTextColors(getResources().getColor(R.color.color_02C557), getResources().getColor(R.color.color_02C557));
            mTabLayout.setSelectedTabIndicatorColor(getResources().getColor(R.color.color_02C557));
            vpFragmentFour.setCurrentItem(1);
        }
    }

    @Override
    public void onError(Throwable throwable) {

    }

    private void initHigh() {
        item_bar = (RelativeLayout.LayoutParams) llState.getLayoutParams();
        item_bar.height = StatusBarUtil.getStatusBarHeight(getActivity());
        llState.setLayoutParams(item_bar);
    }

    public static FourFragment newInstance(int userUid) {
        Bundle bundle = new Bundle();
        FourFragment fragment = new FourFragment();
        fragment.setArguments(bundle);
        return fragment;
    }


}
